﻿using System.Collections.Generic;

namespace FSAG.UserImporter
{
    /// <summary>
    /// Class responsible for managing new status messages, which will be shown in the excel status table
    /// </summary>
    public static class ExcelStatusManager
    {
        static ExcelStatusManager()
        {
            NumberStatusMessages = ConfigManager.GetSettingAsNumber("NumberExcelStatusMessages");
            StatusMessageQueue = new Queue<ExcelStatus>(NumberStatusMessages);
        }

        /// <summary>
        /// Number of status messages which will be shown in the status table
        /// </summary>
        public static int NumberStatusMessages { get; set; }
        
        /// <summary>
        /// Holds the reference of the statusmessagequeue instance
        /// </summary>
        public static Queue<ExcelStatus> StatusMessageQueue { get; set; }

        /// <summary>
        /// Returns the number of currently stored status messages
        /// </summary>
        public static int CountStatusMessages
        {
            get
            {
                return StatusMessageQueue.Count;
            }
        }

        /// <summary>
        /// Returns true, if the status message queue is empty
        /// </summary>
        public static bool IsEmpty
        {
            get
            {
                return StatusMessageQueue.Count==0;
            }
        }

        /// <summary>
        /// Method used to add a new status message to the message queue
        /// </summary>
        /// <param name="message">Message which should be added</param>
        /// <param name="type">Type of the message (info, fehler)</param>
        public static void AddStatusMessage(string message, StatusType type = StatusType.Information)
        {
            if (!string.IsNullOrEmpty(message))
            {
                var statusMessage = new ExcelStatus
                                        {
                                            StatusMessage = message,
                                            StatusType = type
                                        };

                if (StatusMessageQueue.Count == NumberStatusMessages)
                {
                    StatusMessageQueue.Dequeue();
                }

                if (!StatusMessageQueue.Contains(statusMessage))
                {
                    StatusMessageQueue.Enqueue(statusMessage);
                }
            }
        }
    }
}